//
//  CYLTabBarControllerConfig.m
//  SulitLoan
//
//  Created by 虞嘉伟 on 2025/2/25.
//  
  

#import "CYLTabBarControllerConfig.h"
#import "Home1ViewController.h"
#import "Home2ViewController.h"
#import "SLOrderViewController.h"
#import "SLMineViewController.h"


@implementation CYLTabBarControllerConfig

- (CYLTabBarController *)tabBarControllerWithBigCard:(BOOL)bigCard {
    if (_tabBarController == nil) {
        Home1ViewController * first1ViewController = [[Home1ViewController alloc] init];
        first1ViewController.homeModel = self.homeModel;
        Home2ViewController * first2ViewController = [[Home2ViewController alloc] init];
        first2ViewController.homeModel = self.homeModel;
        UIViewController * firstNavigationController = [[UINavigationController alloc] initWithRootViewController:bigCard?first1ViewController:first2ViewController];
          
        SLOrderViewController * secondViewController = [[SLOrderViewController alloc] init];
        UIViewController * secondNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
          
        SLMineViewController * thirdViewController = [[SLMineViewController alloc] init];
        UIViewController * thirdNavigationController = [[UINavigationController alloc] initWithRootViewController:thirdViewController];
          
//        SLMineViewController * fourthViewController = [[SLMineViewController alloc] init];
//        UIViewController * fourthNavigationController = [[BasicNavigationController alloc] initWithRootViewController:fourthViewController];
          
          
        NSArray * tabBarItemsAttributes = [self tabBarItemsAttributes];
        NSArray * viewControllers = @[firstNavigationController, secondNavigationController, thirdNavigationController];
          
        CYLTabBarController * tabBarController = [[CYLTabBarController alloc] init];
          
        tabBarController.tabBarItemsAttributes = tabBarItemsAttributes;
        tabBarController.viewControllers = viewControllers;
          
        _tabBarController = tabBarController;
          
    }
      
    return _tabBarController;
}
  
  
- (NSArray *)tabBarItemsAttributes {
    NSDictionary * tabBarItem1Attribute = @{
                                            CYLTabBarItemTitle : @"Home",
                                            CYLTabBarItemImage : @"home_normal",
                                            //CYLTabBarItemSelectedImage : @"home_highlight"
                                            CYLTabBarItemSelectedImage : @"home_select"
                                   };
    NSDictionary * tabBarItem2Attribute = @{
                                            CYLTabBarItemTitle : @"Order",
                                            CYLTabBarItemImage : @"order_normal",
                                            //CYLTabBarItemSelectedImage : @"mycity_highlight"
                                            CYLTabBarItemSelectedImage : @"order_select"
                                            };
    NSDictionary * tabBarItem3Attribute = @{
                                            CYLTabBarItemTitle : @"Mine",
                                            CYLTabBarItemImage : @"mine_normal",
                                            CYLTabBarItemSelectedImage : @"mine_select"
                                            };
//    NSDictionary * tabBarItem4Attribute = @{
//                                            CYLTabBarItemTitle : @"我的",
//                                            CYLTabBarItemImage : @"mine_normal",
//                                            CYLTabBarItemSelectedImage : @"mine_select"
//                                            };
    NSArray * tarBarItemsAttrbutes = @[tabBarItem1Attribute, tabBarItem2Attribute, tabBarItem3Attribute];
      
    return tarBarItemsAttrbutes;
}
  
  
/**
 *  更多TabBar自定义设置：比如：tabBarItem 的选中和不选中文字和背景图片属性、tabbar 背景图片属性
 */
+ (void)customizeTabBarAppearance {
      
    //去除 TabBar 自带的顶部阴影
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
      
    // set the text color for unselected state
    // 普通状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
      
    // set the text color for selected state
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
      
    // set the text Attributes
    // 设置文字属性
    UITabBarItem *tabBar = [UITabBarItem appearance];
    [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
      
    // Set the dark color to selected tab (the dimmed background)
    // TabBarItem选中后的背景颜色
    [[UITabBar appearance] setSelectionIndicatorImage:[self imageFromColor:[UIColor colorWithRed:26 / 255.0 green:163 / 255.0 blue:133 / 255.0 alpha:1] forSize:CGSizeMake([UIScreen mainScreen].bounds.size.width / 5.0f, 49) withCornerRadius:0]];
      
    // set the bar background color
    // 设置背景图片
    // UITabBar *tabBarAppearance = [UITabBar appearance];
    // [tabBarAppearance setBackgroundImage:[UIImage imageNamed:@"tabbar_background_ios7"]];
}
  
+ (UIImage *)imageFromColor:(UIColor *)color forSize:(CGSize)size withCornerRadius:(CGFloat)radius {
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
      
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
      
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
      
    // Begin a new image that will be the new image with the rounded corners
    // (here with the size of an UIImageView)
    UIGraphicsBeginImageContext(size);
      
    // Add a clip before drawing anything, in the shape of an rounded rect
    [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius] addClip];
    // Draw your image
    [image drawInRect:rect];
      
    // Get the image, here setting the UIImageView image
    image = UIGraphicsGetImageFromCurrentImageContext();
      
    // Lets forget about that we were drawing
    UIGraphicsEndImageContext();
    return image;
}

@end
